(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open FontForge
open FontMetric
open Pycaml
open Python_tools
open UniTools

(*-----------------------------------------------------------------------*)

let unpythonize_font_data =
  unpythonizing_function
    ~expect_tuple:true
    [| AnyType; EitherStringType; EitherStringType |]
    (fun py_args ->
       (FFfont.unpythonize py_args.(0), (* FIX: Type-check this field. *)
        UniText.unpythonize py_args.(1),
        unpythonize_string py_args.(2)))

let open_as_fontforge_font resource_name =
  let kt_font = py_module "kt.font" in
  let args = pytuple_fromsingle (pythonize_string resource_name) in
  let tuple = py_module_call kt_font "open_as_fontforge_font" args in
    unpythonize_font_data tuple

let load_font name load_params =
  if name = "" then
    FontMetric.empty_font
  else
    let (f, font_file, basename) = open_as_fontforge_font name in
      match FFfont.tex_type f with

        | `Unset | `Text    ->
            FFfont.set_enc f "UnicodeBmp";
            FontOT.read_ot f font_file basename load_params

        | `Math  | `MathExt ->
            FontTFM.read_tfm f font_file basename load_params

(*-----------------------------------------------------------------------*)
